<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ZZZ <510299142@qq.com>
// +----------------------------------------------------------------------
namespace plugins\mobile_code_linkai;
use cmf\lib\Plugin;
use think\Db;
/**
 * MobileCodeDemoPlugin
 */
class MobileCodeLinkaiPlugin extends Plugin
{

    public $info = [
        'name'        => 'MobileCodeLinkai',
        'title'       => '凌凯短信注册插件',
        'description' => '凌凯短信注册插件',
        'status'      => 1,
        'author'      => 'ZZZ',
        'version'     => '1.0'
    ];

    public $has_admin = 0;//插件是否有后台管理界面

    public function install() //安装方法必须实现
    {
        return true;//安装成功返回true，失败false
    }

    public function uninstall() //卸载方法必须实现
    {
        return true;//卸载成功返回true，失败false
    }
	
    //实现的send_mobile_verification_code钩子方法
    public function sendMobileVerificationCode($param)
    {
        $mobile        = $param['mobile'];//手机号
        $code          = $param['code'];//验证码
		$code          = rawurlencode(mb_convert_encoding($code, "gb2312", "utf-8"));
        $config        = $this->getConfig();
		$uid           = $config['account_name'];
		$passwd        = $config['account_password'];
        $expire_minute = intval($config['expire_minute']);
        $expire_minute = empty($expire_minute) ? 5 : $expire_minute;
        $expire_time   = time() + $expire_minute * 60;   
        $result        = [];


		$data = array(
            'cdkey' => $uid, // 这里填SN码
            'password' => $passwd, // SN码对应的密码
            'phone' => $mobile, // 手机号
            'message' => '【易房通房产网】您正在注册易房通会员，您的验证码为'.$code,
		);
		
        $gateway  = 'http://hprpt2.eucp.b2m.cn:8080/sdkproxy/sendsms.action?';


		$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $gateway);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
		$response = trim($response);
		$xml = simplexml_load_string($response);
		$result = (string) $xml->error;
		
		if ($result == '0') {
			$result = [
				'error'   => 0,
				'message' => '您的验证码是'.$code,
				'expire_time' => $expire_time
			];
		}else{
			$result = [
				'error'     => 1,
				'message' => '发送失败, 请联系管理员',
				'expire_time' => 0
			];
		}
        return $result;
    }
}